//
//  YBAddCustomGroupSelectPeopleViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBAddCustomGroupSelectPeopleViewController.h"
#import "YBNormalSearchButton.h"
#import "YBWaterLayout.h"
#import "YBNormalWaterCollectionViewCell.h"
#define CELLID @"YBNormalWaterCollectionViewCell"
#import "ZJSqlit.h"
#import "YBPeopleInfoModel.h"
#import "YBSelectHeadModel.h"
#import "LCustomAlertView.h"

#import "YBAddPeopleSignalData.h"
#import "YBSelectPeopleOrganizationView.h"

#import "YBAddCustomGroupViewController.h"

#import "YBSelectPeopleSearchViewController.h"

@interface YBAddCustomGroupSelectPeopleViewController ()<UICollectionViewDelegate , UICollectionViewDataSource ,YBWaterLayoutDelegate , LCustomAlertDelegate , YBSelectPeopleOrganizationViewDelegate , YBSelectPeopleSearchViewControllerDelegate>
@property (nonatomic , strong) YBNormalSearchButton * searchButton;

@property (nonatomic , strong) NSMutableArray * titleLineArray;

@property (nonatomic , strong) UIButton * selectButton;

@property (nonatomic , strong) UILabel * selectNumberLabel;

@property (nonatomic , strong) UICollectionView * collectionView;

@property (nonatomic , strong) YBAddPeopleSignalData * myDataSource;

@property (nonatomic , strong) UIButton * sureButton;

@property (nonatomic , strong) YBSelectPeopleOrganizationView * organizationView;

@property (nonatomic , strong) ZJSqlit * mySqlite;

@property (nonatomic , strong) NSArray * deptArray;

@property (nonatomic , strong) NSArray * userArray;

@property (strong, nonatomic) JCAlertView  *jcAlertView;

@property (nonatomic , assign) NSInteger currentDeleteIndex;


@end


@implementation YBAddCustomGroupSelectPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentDeleteIndex = 0;
    self.deptArray = [NSArray yy_modelArrayWithClass:[YBSelectHeadModel class] json:[self.mySqlite getStringById:YBUSERDATAKEY fromTable:@"ORG_DEPTS"]];
    self.userArray = [NSArray yy_modelArrayWithClass:[YBPeopleInfoModel class] json:[self.mySqlite getStringById:YBUSERDATAKEY fromTable:@"ORG_USERS"]];
    self.titlelabel.text = @"选择人员";
    self.titleLineArray = [NSMutableArray array];
    [self ybInitUserInterface];
}

-(void)dealloc
{
    [YBAddPeopleSignalData attempDealloc];
    self.myDataSource = nil;
}

-(void)ybInitUserInterface{
    
    //头部视图
    UIView * headContentView = [[UIView alloc]initWithFrame:CGRectMake(0, navHeight, [UIScreen zj_screenWith], ItemHeight * 250)];
    headContentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headContentView];
    //搜索按钮
    [headContentView addSubview:self.searchButton];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 80 - 1 , [UIScreen zj_screenWith] , 1)];
    lineView.backgroundColor = [UIColor themeGrayColor];
    [headContentView addSubview:lineView];
    
    //结果展示视图
    [headContentView addSubview:self.selectNumberLabel];
    [headContentView addSubview:self.collectionView];
    
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemHeight * 300, [UIScreen zj_screenWith], ItemHeight * 30)];
    bottomView.backgroundColor = [UIColor themeGrayColor];
    [headContentView addSubview:bottomView];
    
    
    //确定按钮
    [self.view addSubview:self.sureButton];
    
    [self.view addSubview:self.organizationView];
    
    [self.organizationView loadDataWithUserArray:self.userArray deptArray:self.deptArray];
    
    
    
}


-(void)option_searchButton:(UIButton *)sender{
    //搜索按钮
    YBSelectPeopleSearchViewController * vc = [[YBSelectPeopleSearchViewController alloc]init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)option_ensureButton:(UIButton *)sender{
    //确定按钮
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickEnsureButtonWithCurrentSelectArray:)]) {
        [self.delegate clickEnsureButtonWithCurrentSelectArray:[self.myDataSource.selectArray copy]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)action_rightButton:(UIButton *)sender{
    YBAddCustomGroupViewController * addVC = [[YBAddCustomGroupViewController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
}


-(void)option_flowLayoutCellDeleteButton:(UIButton *)sender{
    
    NSInteger index = sender.tag - 1000;
    self.currentDeleteIndex = index;
    YBPeopleInfoModel * tempModel = self.myDataSource.selectArray[index];
    LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:[NSString stringWithFormat:@"是否删除 %@-%@" , [ToolClass returnNoNullStringWithString:tempModel.F_DEPT_NAME] , [ToolClass returnNoNullStringWithString:tempModel.NAME]] titlesArray:@[@"取消",@"确定"]];
    alert.delegate = self;
    self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
    [self.jcAlertView show];
}


-(void)YBSelectPeopleSearchViewControllerDidSelectPeopleModel:(YBPeopleInfoModel *)model
{
    [self.myDataSource ybAddObjectToArrayWithModel:model];
    [self.organizationView ybReloadData];
    [self.collectionView reloadData];
    self.selectNumberLabel.text = [NSString stringWithFormat:@"已选择(%ld人):" , self.myDataSource.selectArray.count];
}





-(void)ybSelectPeopleOrganizationViewSelectedArrayDidChange
{
    [self.collectionView reloadData];
    self.selectNumberLabel.text = [NSString stringWithFormat:@"已选择(%ld人):" , self.myDataSource.selectArray.count];
}


#pragma mark - LCustomAlertDelegate
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    if (index == 1) {
        /**  确认  */
        YBPeopleInfoModel * tempModel = self.myDataSource.selectArray[self.currentDeleteIndex];
        tempModel.ISSELECT = NO;
        [self.myDataSource ybRemoveObjectFromArrayWithModel:tempModel];
        [self.collectionView reloadData];
        [self.organizationView ybReloadData];
        self.selectNumberLabel.text = [NSString stringWithFormat:@"已选择(%ld人):" , self.myDataSource.selectArray.count];
        
    }
    [self.jcAlertView dismissWithCompletion:nil];
}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myDataSource.selectArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBNormalWaterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.isSelectVC = YES;
    cell.model = self.myDataSource.selectArray[indexPath.row];
    cell.deleteButton.tag = 1000 + indexPath.row;
    [cell.deleteButton addTarget:self action:@selector(option_flowLayoutCellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


-(CGFloat)WaterfallFlowLayout:(YBWaterLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.myDataSource.selectArray[indexPath.row];
    return model.selectNameWidth + ItemWidth * 60;
}




-(YBNormalSearchButton *)searchButton
{
    if (!_searchButton) {
        _searchButton = [[YBNormalSearchButton alloc]initWithFrame:CGRectMake(ItemWidth * 100, ItemHeight * 10, [UIScreen zj_screenWith] - ItemWidth * 200, ItemHeight * 60) title:@"输入关键字快速查询内容" imageName:@"soso" imageSize:ItemWidth * 40];
        [_searchButton addTarget:self action:@selector(option_searchButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}


-(UILabel *)selectNumberLabel
{
    if (!_selectNumberLabel) {
        _selectNumberLabel = [ToolClass creatLabelWithFrame:CGRectMake(0, ItemHeight * 80, ItemWidth * 220, ItemHeight * 80) font:15 textColor:[UIColor textGrayColor] text:@"已选择(0人):"];
        _selectNumberLabel.textAlignment = NSTextAlignmentRight;
    }
    return _selectNumberLabel;
}

-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        YBWaterLayout * layout = [[YBWaterLayout alloc]init];
        layout.minimumLineSpacing = ItemHeight * 1;
        layout.minimumInteritemSpacing = ItemWidth * 10;
        layout.sectionInset = UIEdgeInsetsMake(0, ItemWidth * 10, 0 , ItemWidth * 10);
        layout.itemHeight = 75 * ItemHeight;
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(ItemWidth * 220, ItemHeight * 80, [UIScreen zj_screenWith] - ItemWidth * 220, ItemHeight * 140) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[YBNormalWaterCollectionViewCell class] forCellWithReuseIdentifier:CELLID];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
    }
    return _collectionView;
}



-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100) text:@"确定" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _sureButton.backgroundColor = [UIColor themeColor];
        [_sureButton addTarget:self action:@selector(option_ensureButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}



-(YBSelectPeopleOrganizationView *)organizationView
{
    if (!_organizationView) {
        _organizationView = [[YBSelectPeopleOrganizationView alloc]initWithFrame:CGRectMake(0, ItemHeight * 250 + navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - ItemHeight * 350 - navHeight)];
        _organizationView.delegate = self;
        _organizationView.isAddPeople = YES;
    }
    return _organizationView;
}





-(ZJSqlit *)mySqlite
{
    if (!_mySqlite) {
        _mySqlite = [[ZJSqlit alloc]init];
    }
    return _mySqlite;
}


-(YBAddPeopleSignalData *)myDataSource
{
    if (!_myDataSource) {
        _myDataSource = [YBAddPeopleSignalData getYBSelectShareSingalData];
    }
    return _myDataSource;
}

@end
